package org.action;

import java.awt.Color;
import java.awt.Font;

import java.awt.Graphics2D;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.ServletContext;

import org.apache.struts2.ServletActionContext;
import org.bean.O2rankgame;
import org.service.GameService;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.sun.image.codec.jpeg.ImageFormatException;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

@SuppressWarnings("serial")
public class gamescroceimgAction extends ActionSupport {

	private O2rankgame game;
	private int id;
	private GameService gameservice;
	private ByteArrayInputStream imageStream;

	public static BufferedImage createImage(O2rankgame game, String patch) {

		BufferedImage ImageNew = new BufferedImage(800, 600,
				BufferedImage.TYPE_INT_RGB);
		Random r = new Random();

		// 设置背景
		setBGRGB(patch + "gameimage\\bg\\Playing_BG" + r.nextInt(11) + ".bmp",
				ImageNew, 0, 0);

		// 等级
		setFont("100", ImageNew, 401, 162, Color.WHITE);

		// 玩家昵称 判断等级所占长度 401+长度

		setFont(game.getNickname(), ImageNew, 420, 162, Color.yellow);

		// 歌曲名称
		if (game.getMname().length() <= 20) {
			setMusicNameFont(game.getMname(), ImageNew, 445, 550, Color.WHITE);
		} else {
			setMusicNameFont(game.getMname().substring(0, 20) + "...", ImageNew, 445,
					550, Color.WHITE);
		}
		// combo按钮
		setRGB(patch + "gameimage\\combo\\COMBO.png", ImageNew, 68, 149, 0, 0,
				0);

		// 歌曲难度+難度

		String speed = game.getPspeed();

		if (game.getMhard().equals("高级")) {

			if (speed.equals("1")) {
				setRGB(patch + "gameimage\\hardspeed\\HX" + speed + ".bmp",
						ImageNew, 360, 550, 266, 266, 266);
			} else if (speed.equals("1.5")) {
				setRGB(patch + "gameimage\\hardspeed\\HX" + speed + ".bmp",
						ImageNew, 360, 550, 266, 266, 266);
			} else if (speed.equals("2")) {
				setRGB(patch + "gameimage\\hardspeed\\HX" + speed + ".bmp",
						ImageNew, 360, 550, 266, 266, 266);
			} else if (speed.equals("2.5")) {
				setRGB(patch + "gameimage\\hardspeed\\HX" + speed + ".bmp",
						ImageNew, 360, 550, 266, 266, 266);
			} else if (speed.equals("3")) {
				setRGB(patch + "gameimage\\hardspeed\\HX" + speed + ".bmp",
						ImageNew, 360, 550, 266, 266, 266);
			} else if (speed.equals("3.5")) {
				setRGB(patch + "gameimage\\hardspeed\\HX" + speed + ".bmp",
						ImageNew, 360, 550, 266, 266, 266);
			} else if (speed.equals("4")) {
				setRGB(patch + "gameimage\\hardspeed\\HX" + speed + ".bmp",
						ImageNew, 360, 550, 266, 266, 266);
			} else if (speed.equals("4.5")) {
				setRGB(patch + "gameimage\\hardspeed\\HX" + speed + ".bmp",
						ImageNew, 360, 550, 266, 266, 266);
			} else if (speed.equals("5")) {
				setRGB(patch + "gameimage\\hardspeed\\HX" + speed + ".bmp",
						ImageNew, 360, 550, 266, 266, 266);
			} else if (speed.equals("6")) {
				setRGB(patch + "gameimage\\hardspeed\\HX" + speed + ".bmp",
						ImageNew, 360, 550, 266, 266, 266);
			}

			// setRGB(patch + "gameimage\\HX.png", ImageNew, 360, 550, 255, 255,
			// 255);

		} else if (game.getMhard().equals("中级")) {
			if (speed.equals("1")) {
				setRGB(patch + "gameimage\\hardspeed\\NX" + speed + ".bmp",
						ImageNew, 360, 550, 266, 266, 266);
			} else if (speed.equals("1.5")) {
				setRGB(patch + "gameimage\\hardspeed\\NX" + speed + ".bmp",
						ImageNew, 360, 550, 266, 266, 266);
			} else if (speed.equals("2")) {
				setRGB(patch + "gameimage\\hardspeed\\NX" + speed + ".bmp",
						ImageNew, 360, 550, 266, 266, 266);
			} else if (speed.equals("2.5")) {
				setRGB(patch + "gameimage\\hardspeed\\NX" + speed + ".bmp",
						ImageNew, 360, 550, 266, 266, 266);
			} else if (speed.equals("3")) {
				setRGB(patch + "gameimage\\hardspeed\\NX" + speed + ".bmp",
						ImageNew, 360, 550, 266, 266, 266);
			} else if (speed.equals("3.5")) {
				setRGB(patch + "gameimage\\hardspeed\\NX" + speed + ".bmp",
						ImageNew, 360, 550, 266, 266, 266);
			} else if (speed.equals("4")) {
				setRGB(patch + "gameimage\\hardspeed\\NX" + speed + ".bmp",
						ImageNew, 360, 550, 266, 266, 266);
			} else if (speed.equals("4.5")) {
				setRGB(patch + "gameimage\\hardspeed\\NX" + speed + ".bmp",
						ImageNew, 360, 550, 266, 266, 266);
			} else if (speed.equals("5")) {
				setRGB(patch + "gameimage\\hardspeed\\NX" + speed + ".bmp",
						ImageNew, 360, 550, 266, 266, 266);
			} else if (speed.equals("6")) {
				setRGB(patch + "gameimage\\hardspeed\\NX" + speed + ".bmp",
						ImageNew, 360, 550, 266, 266, 266);
			}

		} else {

			if (speed.equals("1")) {
				setRGB(patch + "gameimage\\hardspeed\\EX" + speed + ".bmp",
						ImageNew, 360, 550, 266, 266, 266);
			} else if (speed.equals("1.5")) {
				setRGB(patch + "gameimage\\hardspeed\\EX" + speed + ".bmp",
						ImageNew, 360, 550, 266, 266, 266);
			} else if (speed.equals("2")) {
				setRGB(patch + "gameimage\\hardspeed\\EX" + speed + ".bmp",
						ImageNew, 360, 550, 266, 266, 266);
			} else if (speed.equals("2.5")) {
				setRGB(patch + "gameimage\\hardspeed\\EX" + speed + ".bmp",
						ImageNew, 360, 550, 266, 266, 266);
			} else if (speed.equals("3")) {
				setRGB(patch + "gameimage\\hardspeed\\EX" + speed + ".bmp",
						ImageNew, 360, 550, 266, 266, 266);
			} else if (speed.equals("3.5")) {
				setRGB(patch + "gameimage\\hardspeed\\EX" + speed + ".bmp",
						ImageNew, 360, 550, 266, 266, 266);
			} else if (speed.equals("4")) {
				setRGB(patch + "gameimage\\hardspeed\\EX" + speed + ".bmp",
						ImageNew, 360, 550, 266, 266, 266);
			} else if (speed.equals("4.5")) {
				setRGB(patch + "gameimage\\hardspeed\\EX" + speed + ".bmp",
						ImageNew, 360, 550, 266, 266, 266);
			} else if (speed.equals("5")) {
				setRGB(patch + "gameimage\\hardspeed\\EX" + speed + ".bmp",
						ImageNew, 360, 550, 266, 266, 266);
			} else if (speed.equals("6")) {
				setRGB(patch + "gameimage\\hardspeed\\EX" + speed + ".bmp",
						ImageNew, 360, 550, 266, 266, 266);
			}
		}

		// 连击数
		int combo = game.getCombo();
		int x = String.valueOf(combo).length();
		if (x == 1) {
			setRGB(patch + "gameimage\\combo\\COMBO" + combo + ".png",
					ImageNew, 40, 172, 0, 0, 0);
		} else if (x == 2) {
			setRGB(patch + "gameimage\\combo\\COMBO"
					+ String.valueOf(combo).substring(0, 1) + ".png", ImageNew,
					8, 172, 0, 0, 0);
			setRGB(patch + "gameimage\\combo\\COMBO"
					+ String.valueOf(combo).substring(1, 2) + ".png", ImageNew,
					52, 172, 0, 0, 0);
		} else if (x == 3) {
			setRGB(patch + "gameimage\\combo\\COMBO"
					+ String.valueOf(combo).substring(0, 1) + ".png", ImageNew,
					-4, 172, 0, 0, 0);
			setRGB(patch + "gameimage\\combo\\COMBO"
					+ String.valueOf(combo).substring(1, 2) + ".png", ImageNew,
					40, 172, 0, 0, 0);
			setRGB(patch + "gameimage\\combo\\COMBO"
					+ String.valueOf(combo).substring(2, 3) + ".png", ImageNew,
					84, 172, 0, 0, 0);
		} else if (x == 4) {
			setRGB(patch + "gameimage\\combo\\COMBO"
					+ String.valueOf(combo).substring(0, 1) + ".png", ImageNew,
					-36, 172, 0, 0, 0);
			setRGB(patch + "gameimage\\combo\\COMBO"
					+ String.valueOf(combo).substring(1, 2) + ".png", ImageNew,
					8, 172, 0, 0, 0);
			setRGB(patch + "gameimage\\combo\\COMBO"
					+ String.valueOf(combo).substring(2, 3) + ".png", ImageNew,
					52, 172, 0, 0, 0);
			setRGB(patch + "gameimage\\combo\\COMBO"
					+ String.valueOf(combo).substring(3, 4) + ".png", ImageNew,
					96, 172, 0, 0, 0);
		} else {
			setRGB(patch + "gameimage\\combo\\COMBO9.png", ImageNew, -36, 172,
					0, 0, 0);
			setRGB(patch + "gameimage\\combo\\COMBO9.png", ImageNew, 8, 172, 0,
					0, 0);
			setRGB(patch + "gameimage\\combo\\COMBO9.png", ImageNew, 52, 172,
					0, 0, 0);
			setRGB(patch + "gameimage\\combo\\COMBO9.png", ImageNew, 96, 172,
					0, 0, 0);
		}

		// 绘制歌曲时间

		String time = game.getO2ranksongs().getMtime();
		int t = time.length();
		if (t == 4) {
			setRGB(patch + "gameimage\\time\\"
					+ String.valueOf(time).substring(0, 1) + ".png", ImageNew,
					323, 569, 255, 255, 255);

			setRGB(patch + "gameimage\\time\\"
					+ String.valueOf(time).substring(2, 3) + ".png", ImageNew,
					355, 569, 255, 255, 255);
			setRGB(patch + "gameimage\\time\\"
					+ String.valueOf(time).substring(3, 4) + ".png", ImageNew,
					382, 569, 255, 255, 255);
		} else {
			setRGB(patch + "gameimage\\time\\9.png", ImageNew, 323, 569, 255,
					255, 255);
			setRGB(patch + "gameimage\\time\\6.png", ImageNew, 355, 569, 255,
					255, 255);
			setRGB(patch + "gameimage\\time\\0.png", ImageNew, 382, 569, 255,
					255, 255);
		}
		// 绘制右下角最高连击

		int combonum = game.getCombo();
		int num1 = String.valueOf(combonum).length();
		switch (num1) {
		case 1:
			setRGB(patch + "gameimage\\scorenum\\"
					+ String.valueOf(combonum).substring(0, 1) + ".png",
					ImageNew, 662, 557, 255, 255, 255);
			break;

		case 2:
			setRGB(patch + "gameimage\\scorenum\\"
					+ String.valueOf(combonum).substring(1, 2) + ".png",
					ImageNew, 662, 557, 255, 255, 255);
			setRGB(patch + "gameimage\\scorenum\\"
					+ String.valueOf(combonum).substring(0, 1) + ".png",
					ImageNew, 652, 557, 255, 255, 255);
			break;
		case 3:
			setRGB(patch + "gameimage\\scorenum\\"
					+ String.valueOf(combonum).substring(2, 3) + ".png",
					ImageNew, 662, 557, 255, 255, 255);
			setRGB(patch + "gameimage\\scorenum\\"
					+ String.valueOf(combonum).substring(1, 2) + ".png",
					ImageNew, 652, 557, 255, 255, 255);
			setRGB(patch + "gameimage\\scorenum\\"
					+ String.valueOf(combonum).substring(0, 1) + ".png",
					ImageNew, 642, 557, 255, 255, 255);
			break;
		case 4:
			setRGB(patch + "gameimage\\scorenum\\"
					+ String.valueOf(combonum).substring(3, 4) + ".png",
					ImageNew, 662, 557, 255, 255, 255);
			setRGB(patch + "gameimage\\scorenum\\"
					+ String.valueOf(combonum).substring(2, 3) + ".png",
					ImageNew, 652, 557, 255, 255, 255);
			setRGB(patch + "gameimage\\scorenum\\"
					+ String.valueOf(combonum).substring(1, 2) + ".png",
					ImageNew, 642, 557, 255, 255, 255);
			setRGB(patch + "gameimage\\scorenum\\"
					+ String.valueOf(combonum).substring(0, 1) + ".png",
					ImageNew, 632, 557, 255, 255, 255);
			break;
		case 5:
			setRGB(patch + "gameimage\\scorenum\\"
					+ String.valueOf(combonum).substring(4, 5) + ".png",
					ImageNew, 662, 557, 255, 255, 255);
			setRGB(patch + "gameimage\\scorenum\\"
					+ String.valueOf(combonum).substring(3, 4) + ".png",
					ImageNew, 652, 557, 255, 255, 255);
			setRGB(patch + "gameimage\\scorenum\\"
					+ String.valueOf(combonum).substring(2, 3) + ".png",
					ImageNew, 642, 557, 255, 255, 255);
			setRGB(patch + "gameimage\\scorenum\\"
					+ String.valueOf(combonum).substring(1, 2) + ".png",
					ImageNew, 632, 557, 255, 255, 255);
			setRGB(patch + "gameimage\\scorenum\\"
					+ String.valueOf(combonum).substring(0, 1) + ".png",
					ImageNew, 622, 557, 255, 255, 255);
			break;
		}

		// 绘制右下角COOL

		int cool = game.getCool();
		int coolnum = String.valueOf(cool).length();

		switch (coolnum) {
		case 1:
			setRGB(patch + "gameimage\\scorenum\\"
					+ String.valueOf(cool).substring(0, 1) + ".png", ImageNew,
					648, 571, 255, 255, 255);
			break;

		case 2:
			setRGB(patch + "gameimage\\scorenum\\"
					+ String.valueOf(cool).substring(1, 2) + ".png", ImageNew,
					648, 571, 255, 255, 255);
			setRGB(patch + "gameimage\\scorenum\\"
					+ String.valueOf(cool).substring(0, 1) + ".png", ImageNew,
					638, 571, 255, 255, 255);
			break;
		case 3:
			setRGB(patch + "gameimage\\scorenum\\"
					+ String.valueOf(cool).substring(2, 3) + ".png", ImageNew,
					648, 571, 255, 255, 255);
			setRGB(patch + "gameimage\\scorenum\\"
					+ String.valueOf(cool).substring(1, 2) + ".png", ImageNew,
					638, 571, 255, 255, 255);
			setRGB(patch + "gameimage\\scorenum\\"
					+ String.valueOf(cool).substring(0, 1) + ".png", ImageNew,
					628, 571, 255, 255, 255);
			break;
		case 4:
			setRGB(patch + "gameimage\\scorenum\\"
					+ String.valueOf(cool).substring(3, 4) + ".png", ImageNew,
					648, 571, 255, 255, 255);
			setRGB(patch + "gameimage\\scorenum\\"
					+ String.valueOf(cool).substring(2, 3) + ".png", ImageNew,
					638, 571, 255, 255, 255);
			setRGB(patch + "gameimage\\scorenum\\"
					+ String.valueOf(cool).substring(1, 2) + ".png", ImageNew,
					628, 571, 255, 255, 255);
			setRGB(patch + "gameimage\\scorenum\\"
					+ String.valueOf(cool).substring(0, 1) + ".png", ImageNew,
					618, 571, 255, 255, 255);
			break;
		case 5:
			setRGB(patch + "gameimage\\scorenum\\"
					+ String.valueOf(cool).substring(4, 5) + ".png", ImageNew,
					648, 571, 255, 255, 255);
			setRGB(patch + "gameimage\\scorenum\\"
					+ String.valueOf(cool).substring(3, 4) + ".png", ImageNew,
					638, 571, 255, 255, 255);
			setRGB(patch + "gameimage\\scorenum\\"
					+ String.valueOf(cool).substring(2, 3) + ".png", ImageNew,
					628, 571, 255, 255, 255);
			setRGB(patch + "gameimage\\scorenum\\"
					+ String.valueOf(cool).substring(1, 2) + ".png", ImageNew,
					618, 571, 255, 255, 255);
			setRGB(patch + "gameimage\\scorenum\\"
					+ String.valueOf(cool).substring(0, 1) + ".png", ImageNew,
					608, 571, 255, 255, 255);
			break;
		}

		// 绘制右下角good

		int good = game.getGood();
		int goodnum = String.valueOf(good).length();

		switch (goodnum) {
		case 1:
			setRGB(patch + "gameimage\\scorenum\\"
					+ String.valueOf(good).substring(0, 1) + ".png", ImageNew,
					718, 571, 255, 255, 255);
			break;

		case 2:
			setRGB(patch + "gameimage\\scorenum\\"
					+ String.valueOf(good).substring(1, 2) + ".png", ImageNew,
					718, 571, 255, 255, 255);
			setRGB(patch + "gameimage\\scorenum\\"
					+ String.valueOf(good).substring(0, 1) + ".png", ImageNew,
					708, 571, 255, 255, 255);
			break;
		case 3:
			setRGB(patch + "gameimage\\scorenum\\"
					+ String.valueOf(good).substring(2, 3) + ".png", ImageNew,
					718, 571, 255, 255, 255);
			setRGB(patch + "gameimage\\scorenum\\"
					+ String.valueOf(good).substring(1, 2) + ".png", ImageNew,
					708, 571, 255, 255, 255);
			setRGB(patch + "gameimage\\scorenum\\"
					+ String.valueOf(good).substring(0, 1) + ".png", ImageNew,
					698, 571, 255, 255, 255);
			break;
		case 4:
			setRGB(patch + "gameimage\\scorenum\\"
					+ String.valueOf(good).substring(3, 4) + ".png", ImageNew,
					718, 571, 255, 255, 255);
			setRGB(patch + "gameimage\\scorenum\\"
					+ String.valueOf(good).substring(2, 3) + ".png", ImageNew,
					708, 571, 255, 255, 255);
			setRGB(patch + "gameimage\\scorenum\\"
					+ String.valueOf(good).substring(1, 2) + ".png", ImageNew,
					698, 571, 255, 255, 255);
			setRGB(patch + "gameimage\\scorenum\\"
					+ String.valueOf(good).substring(0, 1) + ".png", ImageNew,
					688, 571, 255, 255, 255);
			break;
		case 5:
			setRGB(patch + "gameimage\\scorenum\\"
					+ String.valueOf(good).substring(4, 5) + ".png", ImageNew,
					718, 571, 255, 255, 255);
			setRGB(patch + "gameimage\\scorenum\\"
					+ String.valueOf(good).substring(3, 4) + ".png", ImageNew,
					708, 571, 255, 255, 255);
			setRGB(patch + "gameimage\\scorenum\\"
					+ String.valueOf(good).substring(2, 3) + ".png", ImageNew,
					698, 571, 255, 255, 255);
			setRGB(patch + "gameimage\\scorenum\\"
					+ String.valueOf(good).substring(1, 2) + ".png", ImageNew,
					688, 571, 255, 255, 255);
			setRGB(patch + "gameimage\\scorenum\\"
					+ String.valueOf(good).substring(0, 1) + ".png", ImageNew,
					678, 571, 255, 255, 255);
			break;
		}

		// 绘制左下角BAD
		int bad = game.getBad();
		int badnum = String.valueOf(bad).length();

		switch (badnum) {
		case 1:
			setRGB(patch + "gameimage\\scorenum\\"
					+ String.valueOf(bad).substring(0, 1) + ".png", ImageNew,
					648, 581, 255, 255, 255);
			break;

		case 2:
			setRGB(patch + "gameimage\\scorenum\\"
					+ String.valueOf(bad).substring(1, 2) + ".png", ImageNew,
					648, 581, 255, 255, 255);
			setRGB(patch + "gameimage\\scorenum\\"
					+ String.valueOf(bad).substring(0, 1) + ".png", ImageNew,
					638, 581, 255, 255, 255);
			break;
		case 3:
			setRGB(patch + "gameimage\\scorenum\\"
					+ String.valueOf(bad).substring(2, 3) + ".png", ImageNew,
					648, 581, 255, 255, 255);
			setRGB(patch + "gameimage\\scorenum\\"
					+ String.valueOf(bad).substring(1, 2) + ".png", ImageNew,
					638, 581, 255, 255, 255);
			setRGB(patch + "gameimage\\scorenum\\"
					+ String.valueOf(bad).substring(0, 1) + ".png", ImageNew,
					628, 581, 255, 255, 255);
			break;
		case 4:
			setRGB(patch + "gameimage\\scorenum\\"
					+ String.valueOf(bad).substring(3, 4) + ".png", ImageNew,
					648, 581, 255, 255, 255);
			setRGB(patch + "gameimage\\scorenum\\"
					+ String.valueOf(bad).substring(2, 3) + ".png", ImageNew,
					638, 581, 255, 255, 255);
			setRGB(patch + "gameimage\\scorenum\\"
					+ String.valueOf(bad).substring(1, 2) + ".png", ImageNew,
					628, 581, 255, 255, 255);
			setRGB(patch + "gameimage\\scorenum\\"
					+ String.valueOf(bad).substring(0, 1) + ".png", ImageNew,
					618, 581, 255, 255, 255);
			break;
		case 5:
			setRGB(patch + "gameimage\\scorenum\\"
					+ String.valueOf(bad).substring(4, 5) + ".png", ImageNew,
					648, 581, 255, 255, 255);
			setRGB(patch + "gameimage\\scorenum\\"
					+ String.valueOf(bad).substring(3, 4) + ".png", ImageNew,
					638, 581, 255, 255, 255);
			setRGB(patch + "gameimage\\scorenum\\"
					+ String.valueOf(bad).substring(2, 3) + ".png", ImageNew,
					628, 581, 255, 255, 255);
			setRGB(patch + "gameimage\\scorenum\\"
					+ String.valueOf(bad).substring(1, 2) + ".png", ImageNew,
					618, 581, 255, 255, 255);
			setRGB(patch + "gameimage\\scorenum\\"
					+ String.valueOf(bad).substring(0, 1) + ".png", ImageNew,
					608, 581, 255, 255, 255);
			break;
		}

		// 绘制左下角miss
		int miss = game.getMiss();
		int missnum = String.valueOf(miss).length();
		switch (missnum) {
		case 1:
			setRGB(patch + "gameimage\\scorenum\\"
					+ String.valueOf(miss).substring(0, 1) + ".png", ImageNew,
					718, 581, 255, 255, 255);
			break;

		case 2:
			setRGB(patch + "gameimage\\scorenum\\"
					+ String.valueOf(miss).substring(1, 2) + ".png", ImageNew,
					718, 581, 255, 255, 255);
			setRGB(patch + "gameimage\\scorenum\\"
					+ String.valueOf(miss).substring(0, 1) + ".png", ImageNew,
					708, 581, 255, 255, 255);
			break;
		case 3:
			setRGB(patch + "gameimage\\scorenum\\"
					+ String.valueOf(miss).substring(2, 3) + ".png", ImageNew,
					718, 581, 255, 255, 255);
			setRGB(patch + "gameimage\\scorenum\\"
					+ String.valueOf(miss).substring(1, 2) + ".png", ImageNew,
					708, 581, 255, 255, 255);
			setRGB(patch + "gameimage\\scorenum\\"
					+ String.valueOf(miss).substring(0, 1) + ".png", ImageNew,
					698, 581, 255, 255, 255);
			break;
		case 4:
			setRGB(patch + "gameimage\\scorenum\\"
					+ String.valueOf(miss).substring(3, 4) + ".png", ImageNew,
					718, 581, 255, 255, 255);
			setRGB(patch + "gameimage\\scorenum\\"
					+ String.valueOf(miss).substring(2, 3) + ".png", ImageNew,
					708, 581, 255, 255, 255);
			setRGB(patch + "gameimage\\scorenum\\"
					+ String.valueOf(miss).substring(1, 2) + ".png", ImageNew,
					698, 581, 255, 255, 255);
			setRGB(patch + "gameimage\\scorenum\\"
					+ String.valueOf(miss).substring(0, 1) + ".png", ImageNew,
					688, 581, 255, 255, 255);
			break;
		case 5:
			setRGB(patch + "gameimage\\scorenum\\"
					+ String.valueOf(miss).substring(4, 5) + ".png", ImageNew,
					718, 581, 255, 255, 255);
			setRGB(patch + "gameimage\\scorenum\\"
					+ String.valueOf(miss).substring(3, 4) + ".png", ImageNew,
					708, 581, 255, 255, 255);
			setRGB(patch + "gameimage\\scorenum\\"
					+ String.valueOf(miss).substring(2, 3) + ".png", ImageNew,
					698, 581, 255, 255, 255);
			setRGB(patch + "gameimage\\scorenum\\"
					+ String.valueOf(miss).substring(1, 2) + ".png", ImageNew,
					688, 581, 255, 255, 255);
			setRGB(patch + "gameimage\\scorenum\\"
					+ String.valueOf(miss).substring(0, 1) + ".png", ImageNew,
					678, 581, 255, 255, 255);
			break;
		}

		// 绘制分数
		int score = game.getScore();
		int y = String.valueOf(score).length();

		switch (y) {
		case 1:
			setRGB(patch + "gameimage\\score\\"
					+ String.valueOf(score).substring(0, 1) + ".png", ImageNew,
					175, 566, 266, 266, 266);

			break;

		case 2:

			setRGB(patch + "gameimage\\score\\"
					+ String.valueOf(score).substring(1, 2) + ".png", ImageNew,
					175, 566, 266, 266, 266);
			setRGB(patch + "gameimage\\score\\"
					+ String.valueOf(score).substring(0, 1) + ".png", ImageNew,
					153, 566, 266, 266, 266);

			break;

		case 3:
			setRGB(patch + "gameimage\\score\\"
					+ String.valueOf(score).substring(2, 3) + ".png", ImageNew,
					175, 566, 266, 266, 266);
			setRGB(patch + "gameimage\\score\\"
					+ String.valueOf(score).substring(1, 2) + ".png", ImageNew,
					153, 566, 266, 266, 266);
			setRGB(patch + "gameimage\\score\\"
					+ String.valueOf(score).substring(0, 1) + ".png", ImageNew,
					131, 566, 266, 266, 266);

			break;

		case 4:
			setRGB(patch + "gameimage\\score\\"
					+ String.valueOf(score).substring(3, 4) + ".png", ImageNew,
					175, 566, 266, 266, 266);
			setRGB(patch + "gameimage\\score\\"
					+ String.valueOf(score).substring(2, 3) + ".png", ImageNew,
					153, 566, 266, 266, 266);
			setRGB(patch + "gameimage\\score\\"
					+ String.valueOf(score).substring(1, 2) + ".png", ImageNew,
					131, 566, 266, 266, 266);
			setRGB(patch + "gameimage\\score\\"
					+ String.valueOf(score).substring(0, 1) + ".png", ImageNew,
					109, 566, 266, 266, 266);
			break;
		case 5:
			setRGB(patch + "gameimage\\score\\"
					+ String.valueOf(score).substring(4, 5) + ".png", ImageNew,
					175, 566, 266, 266, 266);
			setRGB(patch + "gameimage\\score\\"
					+ String.valueOf(score).substring(3, 4) + ".png", ImageNew,
					153, 566, 266, 266, 266);
			setRGB(patch + "gameimage\\score\\"
					+ String.valueOf(score).substring(2, 3) + ".png", ImageNew,
					131, 566, 266, 266, 266);
			setRGB(patch + "gameimage\\score\\"
					+ String.valueOf(score).substring(1, 2) + ".png", ImageNew,
					109, 566, 266, 266, 266);
			setRGB(patch + "gameimage\\score\\"
					+ String.valueOf(score).substring(0, 1) + ".png", ImageNew,
					87, 566, 266, 266, 266);
			break;
		case 6:
			setRGB(patch + "gameimage\\score\\"
					+ String.valueOf(score).substring(5, 6) + ".png", ImageNew,
					175, 566, 266, 266, 266);
			setRGB(patch + "gameimage\\score\\"
					+ String.valueOf(score).substring(4, 5) + ".png", ImageNew,
					153, 566, 266, 266, 266);
			setRGB(patch + "gameimage\\score\\"
					+ String.valueOf(score).substring(3, 4) + ".png", ImageNew,
					131, 566, 266, 266, 266);
			setRGB(patch + "gameimage\\score\\"
					+ String.valueOf(score).substring(2, 3) + ".png", ImageNew,
					109, 566, 266, 266, 266);
			setRGB(patch + "gameimage\\score\\"
					+ String.valueOf(score).substring(1, 2) + ".png", ImageNew,
					87, 566, 266, 266, 266);
			setRGB(patch + "gameimage\\score\\"
					+ String.valueOf(score).substring(0, 1) + ".png", ImageNew,
					65, 566, 266, 266, 266);
			break;
		case 7:
			setRGB(patch + "gameimage\\score\\"
					+ String.valueOf(score).substring(6, 7) + ".png", ImageNew,
					175, 566, 266, 266, 266);
			setRGB(patch + "gameimage\\score\\"
					+ String.valueOf(score).substring(5, 6) + ".png", ImageNew,
					153, 566, 266, 266, 266);
			setRGB(patch + "gameimage\\score\\"
					+ String.valueOf(score).substring(4, 5) + ".png", ImageNew,
					131, 566, 266, 266, 266);
			setRGB(patch + "gameimage\\score\\"
					+ String.valueOf(score).substring(3, 4) + ".png", ImageNew,
					109, 566, 266, 266, 266);
			setRGB(patch + "gameimage\\score\\"
					+ String.valueOf(score).substring(2, 3) + ".png", ImageNew,
					87, 566, 266, 266, 266);
			setRGB(patch + "gameimage\\score\\"
					+ String.valueOf(score).substring(1, 2) + ".png", ImageNew,
					65, 566, 266, 266, 266);
			setRGB(patch + "gameimage\\score\\"
					+ String.valueOf(score).substring(0, 1) + ".png", ImageNew,
					43, 566, 266, 266, 266);
			break;
		case 8:
			setRGB(patch + "gameimage\\score\\"
					+ String.valueOf(score).substring(7, 8) + ".png", ImageNew,
					175, 566, 266, 266, 266);
			setRGB(patch + "gameimage\\score\\"
					+ String.valueOf(score).substring(6, 7) + ".png", ImageNew,
					153, 566, 266, 266, 266);
			setRGB(patch + "gameimage\\score\\"
					+ String.valueOf(score).substring(5, 6) + ".png", ImageNew,
					131, 566, 266, 266, 266);
			setRGB(patch + "gameimage\\score\\"
					+ String.valueOf(score).substring(4, 5) + ".png", ImageNew,
					109, 566, 266, 266, 266);
			setRGB(patch + "gameimage\\score\\"
					+ String.valueOf(score).substring(3, 4) + ".png", ImageNew,
					87, 566, 266, 266, 266);
			setRGB(patch + "gameimage\\score\\"
					+ String.valueOf(score).substring(2, 3) + ".png", ImageNew,
					65, 566, 266, 266, 266);
			setRGB(patch + "gameimage\\score\\"
					+ String.valueOf(score).substring(1, 2) + ".png", ImageNew,
					43, 566, 266, 266, 266);
			setRGB(patch + "gameimage\\score\\"
					+ String.valueOf(score).substring(0, 1) + ".png", ImageNew,
					21, 566, 266, 266, 266);
			break;
		case 9:
			setRGB(patch + "gameimage\\score\\"
					+ String.valueOf(score).substring(8, 9) + ".png", ImageNew,
					175, 566, 266, 266, 266);
			setRGB(patch + "gameimage\\score\\"
					+ String.valueOf(score).substring(7, 8) + ".png", ImageNew,
					153, 566, 266, 266, 266);
			setRGB(patch + "gameimage\\score\\"
					+ String.valueOf(score).substring(6, 7) + ".png", ImageNew,
					131, 566, 266, 266, 266);
			setRGB(patch + "gameimage\\score\\"
					+ String.valueOf(score).substring(5, 6) + ".png", ImageNew,
					109, 566, 266, 266, 266);
			setRGB(patch + "gameimage\\score\\"
					+ String.valueOf(score).substring(4, 5) + ".png", ImageNew,
					87, 566, 266, 266, 266);
			setRGB(patch + "gameimage\\score\\"
					+ String.valueOf(score).substring(3, 4) + ".png", ImageNew,
					65, 566, 266, 266, 266);
			setRGB(patch + "gameimage\\score\\"
					+ String.valueOf(score).substring(2, 3) + ".png", ImageNew,
					43, 566, 266, 266, 266);
			setRGB(patch + "gameimage\\score\\"
					+ String.valueOf(score).substring(1, 2) + ".png", ImageNew,
					21, 566, 266, 266, 266);
			setRGB(patch + "gameimage\\score\\"
					+ String.valueOf(score).substring(0, 1) + ".png", ImageNew,
					-1, 566, 266, 266, 266);

			break;
		}

		return ImageNew;
	}

	/**
	 * 将BufferedImage转换成ByteArrayInputStream
	 * 
	 * @param image
	 *            图片
	 * @return ByteArrayInputStream 流
	 */
	private static ByteArrayInputStream convertImageToStream(BufferedImage image) {
		ByteArrayInputStream inputStream = null;
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		JPEGImageEncoder jpeg = JPEGCodec.createJPEGEncoder(bos);
		try {
			jpeg.encode(image);
			byte[] bts = bos.toByteArray();
			inputStream = new ByteArrayInputStream(bts);
		} catch (ImageFormatException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return inputStream;
	}

	@Override
	public String execute() throws Exception {

		game = gameservice.SelectByid(id);

		ActionContext ac = ActionContext.getContext();
		ServletContext sc = (ServletContext) ac
				.get(ServletActionContext.SERVLET_CONTEXT);
		String path = sc.getRealPath("/");

		imageStream = convertImageToStream(createImage(game, path));

		return SUCCESS;
	}

	public static void setBGRGB(String patch, BufferedImage ImageNew, int x,
			int y) {

		try {
			// 读取图片
			File fileBG = new File(patch);
			BufferedImage ImageBG = ImageIO.read(fileBG);

			int BGwidth = ImageBG.getWidth();// 图片宽度
			int BGheight = ImageBG.getHeight();// 图片高度

			// 读取RGB
			int[] ImageArrayBG = new int[BGwidth * BGheight];
			ImageArrayBG = ImageBG.getRGB(0, 0, BGwidth, BGheight,
					ImageArrayBG, 0, BGwidth);
			ImageNew.setRGB(x, y, BGwidth, BGheight, ImageArrayBG, 0, BGwidth);// 设置RGB

		} catch (Exception e) {
			System.out.println(e);
		}
	}

	public static void setRGB(String patch, BufferedImage ImageNew, int x,
			int y, int r, int g, int b) {

		try {

			File fileBG = new File(patch);
			BufferedImage ImageBG = ImageIO.read(fileBG);

			int BGwidth = ImageBG.getWidth();
			int BGheight = ImageBG.getHeight();

			int[] ImageArrayBG = new int[BGwidth * BGheight];
			ImageArrayBG = ImageBG.getRGB(0, 0, BGwidth, BGheight,
					ImageArrayBG, 0, BGwidth);

			for (int i = 0; i < BGwidth; i++) {
				for (int j = 0; j < BGwidth; j++) {
					if (j >= BGheight) {
						break;
					}
					int rgb = ImageBG.getRGB(i, j);

					int R = (rgb & 0xff0000) >> 16;
					int G = (rgb & 0xff00) >> 8;
					int B = (rgb & 0xff);
					rgb = ((R * 256) + G) * 256 + B;
					if (R != r && G != g && B != b) {
						ImageNew.setRGB(i + x, j + y, rgb);
					}
				}
			}

		} catch (Exception e) {
			System.out.println(e);
		}
	}

	public static void setFont(String context, BufferedImage ImageNew, int x,
			int y, Color c) {

		Graphics2D g = ImageNew.createGraphics();// 得到图形上下文
		
		
		
		g.setFont(new Font("宋体", Font.PLAIN, 12));// 设置字体
		
		g.setColor(Color.BLACK); // 设置画笔颜色 Color.LIGHT_GRAY
		
		g.drawString(context, ShiftWest(x, 1), ShiftNorth(y, 1));
		g.drawString(context, ShiftWest(x, 1), ShiftSouth(y, 1));
		g.drawString(context, ShiftEast(x, 1), ShiftNorth(y, 1));
		g.drawString(context, ShiftEast(x, 1), ShiftSouth(y, 1));

		
		
		g.setColor(c); 
		
	
		g.drawString(context, x, y);

		
		
		g.dispose();

	}
	public static void setMusicNameFont(String context, BufferedImage ImageNew, int x,
			int y, Color c) {

		Graphics2D g = ImageNew.createGraphics();// 得到图形上下文
		
		
		
		g.setFont(new Font("宋体", Font.PLAIN, 12));// 设置字体

		g.setColor(c); 
	
		g.drawString(context, x, y);

		g.dispose();

	}
	
	
	static int ShiftNorth(int p, int distance) {
		   return (p - distance);
		   }
		static int ShiftSouth(int p, int distance) {
		   return (p + distance);
		   }
		static int ShiftEast(int p, int distance) {
		   return (p + distance);
		   }
		static int ShiftWest(int p, int distance) {
		   return (p - distance);
		   }
	
	
	public O2rankgame getGame() {
		return game;
	}

	public void setGame(O2rankgame game) {
		this.game = game;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public GameService getGameservice() {
		return gameservice;
	}

	public void setGameservice(GameService gameservice) {
		this.gameservice = gameservice;
	}

	public ByteArrayInputStream getImageStream() {
		return imageStream;
	}

	public void setImageStream(ByteArrayInputStream imageStream) {
		this.imageStream = imageStream;
	}

}
